package cn.community.care.module.system.controller.admin.orders;

import org.springframework.web.bind.annotation.*;

import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;


import java.util.*;
import java.io.IOException;

import cn.community.care.framework.common.pojo.PageParam;
import cn.community.care.framework.common.pojo.PageResult;
import cn.community.care.framework.common.pojo.CommonResult;
import cn.community.care.framework.common.util.object.BeanUtils;
import static cn.community.care.framework.common.pojo.CommonResult.success;

import cn.community.care.framework.excel.core.util.ExcelUtils;

import cn.community.care.framework.operatelog.core.annotations.OperateLog;
import static cn.community.care.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.community.care.module.system.controller.admin.orders.vo.*;
import cn.community.care.module.system.dal.dataobject.orders.OrdersDO;
import cn.community.care.module.system.service.orders.OrdersService;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

@Tag(name = "管理后台 - 订单")
@RestController
@RequestMapping("/system/orders")
@Validated
public class OrdersController {

    @Resource
    private OrdersService ordersService;

    @PostMapping("/create")
    @Operation(summary = "创建订单")
    public CommonResult<Long> createOrders(@Valid @RequestBody OrdersSaveReqVO createReqVO) {
        return success(ordersService.createOrders(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新订单")
    public CommonResult<Boolean> updateOrders(@Valid @RequestBody OrdersSaveReqVO updateReqVO) {
        ordersService.updateOrders(updateReqVO);
        return success(true);
    }

    @PutMapping("/update/service")
    @Operation(summary = "更新订单")
    public CommonResult<Boolean> updateOrdersByService(@Valid @RequestBody OrdersSaveReqVO updateReqVO) {
        ordersService.updateOrdersByService(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除订单")
    @Parameter(name = "id", description = "编号", required = true)
    public CommonResult<Boolean> deleteOrders(@RequestParam("id") Long id) {
        ordersService.deleteOrders(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得订单")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    public CommonResult<OrdersRespVO> getOrders(@RequestParam("id") Long id) {
        OrdersDO orders = ordersService.getOrders(id);
        return success(BeanUtils.toBean(orders, OrdersRespVO.class));
    }

    @PostMapping("/page")
    @Operation(summary = "获得订单分页")
    public CommonResult<PageResult<OrdersRespVO>> getOrdersPage(@Valid @RequestBody OrdersPageReqVO pageReqVO) {
        if (pageReqVO.getIsAllData()){
            pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        }
        PageResult<OrdersDO> pageResult = ordersService.getOrdersPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, OrdersRespVO.class));
    }
    @PostMapping("/page/byCrt")
    @Operation(summary = "根据购物车获得订单")
    public CommonResult<PageResult<OrdersRespVO>> getOrdersPageByCart(@Valid @RequestBody OrdersPageReqVO pageReqVO) {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        PageResult<OrdersDO> pageResult = ordersService.getOrdersPageByCart(pageReqVO);
        return success(BeanUtils.toBean(pageResult, OrdersRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出订单 Excel")
    @OperateLog(type = EXPORT)
    public void exportOrdersExcel(@Valid OrdersPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<OrdersDO> list = ordersService.getOrdersPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "订单.xls", "数据", OrdersRespVO.class,
                        BeanUtils.toBean(list, OrdersRespVO.class));
    }

    @GetMapping("/trend")
    @Operation(summary = "统计销售趋势")
    public CommonResult<List<Map<String, Object>>> getArticlePublishTrend() {
        return success(ordersService.countTotalPriceByDayInLast7Days());
    }

    @GetMapping("/price/count")
    @Operation(summary = "统计总金额数量")
    public CommonResult<Double> getArticleCount() {
        Double count = ordersService.getOrdersCount();
        return success(count);
    }

    @GetMapping("/count")
    @Operation(summary = "统计总金额数量")
    public CommonResult<Long> gettOrdersCount() {
        Long count = ordersService.getCount();
        return success(count);
    }

}